{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# python命名空间"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 先看个例子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# %load ..\\data\\demo1\\A.py\n",
    "import main\n",
    "\n",
    "def A_fun1():\n",
    "    print main.main_gx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# %load ..\\data\\demo1\\main.py\n",
    "main_gx = 1\n",
    "\n",
    "\n",
    "def main_fun1():\n",
    "    global main_gx\n",
    "    main_gx = 100\n",
    "    import A\n",
    "    A.A_fun1() #1\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main_fun1()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "demo1模块main.py中函数main_fun1改变了main_gx了值100，但模块中.py函数A_fun1显示main_gx还是1，这是为什么？难道全局变量的范围只限于本模块吗？\n",
    "1. 全局变量可以在其他模块中使用，本例中main.py模块是主入口模块，比较特殊。python解释器启动后，首先加载main.py，其后在运行过程中import A，导入模块A，在A中又再次import main，导致main_gx重新设置为1；\n",
    "2. 模块只能导入一次，python解释器启动的主入口模块不算导入：(，所以其他非入口模块不会有这样的问题；\n",
    "3. 不建议在入口模块中放置共享变量。如果实在要用，放入公共模块中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# %load ..\\data\\demo2\\once.py\n",
    "print 'once' #只导入一次"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# %load ..\\data\\demo2\\second.py\n",
    "import once"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# %load ..\\data\\demo2\\main.py\n",
    "import once   #导入模块once.py\n",
    "import second #模块second.py再次import once,不再导入\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 命名空间分为三种：内置命名空间、全局命名空间、局部命名空间\n",
    "1、每个模块，包含一个全局命名空间，通过globals()查看；\n",
    "\n",
    "2、每个函数，1，通过locals()查看；但在函数外调用locals()，基本等同于globals()；\n",
    "\n",
    "3、每个全局命名空间，又引用了内置命名空间\\__builtin\\__，等于预设了一些变量；"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 包和普通目录的区别\n",
    "1、包其实也是目录，但在目录下存在\\__init\\__.py模块，导入包时首先导入该模块；\n",
    "\n",
    "2、普通目录没有该模块，如果需要导入该目录下的模块，需把路径加入python搜索路径中；"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## import x模块和from x模块 import \\*的区别\n",
    "1、import x, 首先创建了x模块的全局命名空间，同时在本模块的全局命名空间中创建了x变量指向x模块的全局命名空间，注意这里是指向，类似指针，**如果模块间需要共享变量，采用这种方式**；\n",
    "\n",
    "2、from x import \\*，同样首先创建了x模块的全局命名空间，同时拷贝了一份到本模块的全局命名空间里，注意这里是拷贝，两模块存在同名变量，但不互相干扰，**如果是包，\\__init\\__.py中一般采用这种方式,通过拷贝方式统一导到__init__模块中，再由它统一暴露到包外**；"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# %load ..\\data\\demo3\\A.py\n",
    "def A_fun():\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# %load ..\\data\\demo3\\B.py\n",
    "def B_fun():\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# %load ..\\data\\demo3\\main.py\n",
    "if __name__ == '__main__':\n",
    "    import A\n",
    "    print globals()  #{'A': <module 'A' from 'D:\\src\\about-python\\data\\demo3\\A.py'>,...}\n",
    "    from B import *\n",
    "    print globals()  #{'B_fun': <function B_fun at 0x000000000264D5F8>,...}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## import 包、import 包.模块、from 包 import \\* ...\n",
    "1、import package  => import package.\\__init__\n",
    "\n",
    "2、from package import \\*  => from package.\\__init\\__ import \\*\n",
    "\n",
    "3、import package.module  => import package.\\__init__， import module\n",
    "\n",
    "4、from package.module import \\*  => from package.\\__init\\__ import \\*，from module import \\*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 模块中包含\\__all\\__，会影响from module import *的导入，但不影响import module\n",
    "1、from module import *方式，首先不会导入以\\_开头的变量及函数，这个是约定；\n",
    "\n",
    "2、from module import *方式，如果定义了\\__all\\__，只能导入\\__all\\__指定的变量及函数"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
